// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) braces deadcode fieldsfirst 

package net.minecraft;


import java.awt.*;
import java.awt.event.*;
import java.awt.image.VolatileImage;
import java.io.*;
import java.net.URL;
import java.util.Random;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import javax.imageio.ImageIO;

// Referenced classes of package net.minecraft:
//            MinecraftUtil, LauncherFrame

public class LoginForm extends Panel
{

    private static final long serialVersionUID = 1L;
    private Image bgImage;
    private TextField userName;
    private Checkbox forceUpdateBox;
    private Button launchButton;
    private Label errorLabel;
    private Label creditsVersion;
    private Button openSettings;
    private Choice boxcombo;
    private LauncherFrame launcherFrame;
    private boolean outdated;
    private VolatileImage img;
    public LoginForm(LauncherFrame launcherframe)
    {
       
        userName = new TextField(20);
        forceUpdateBox = new Checkbox("Force Update");
        launchButton = new Button("Enter Game");
        errorLabel = new Label("", 1);
        creditsVersion = new Label("v13 <djmati11's edition>");
        openSettings = new Button("Settings");
        outdated = false;
        boxcombo = new Choice();
        FilenameFilter filter = new FilenameFilter() {
        	    public boolean accept(File dir, String name) {
        	    	if (name.contains("lwjgl.jar") || name.contains("jinput.jar") || name.contains("lwjgl_util.jar"))
        	    	{
        	    		return false;
        	    	} else {
        	    		return name.endsWith(".jar");
        	    	}
        	    }
        	};
        String[] files = MinecraftUtil.getBinFolder().list(filter);
        for (String file : files)
        {
            
            boxcombo.add(file);
        	
        }
        launcherFrame = launcherframe;
        GridBagLayout gridbaglayout = new GridBagLayout();
        setLayout(gridbaglayout);
        add(buildLoginPanel());
        try
        {
            bgImage = ImageIO.read((net.minecraft.LoginForm.class).getResource("dirt.png")).getScaledInstance(32, 32, 16);
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
        }
        readUsername();
        launchButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
            	GameUpdater.gameFiles[3] = boxcombo.getItem(boxcombo.getSelectedIndex());
            			
                if(forceUpdateBox.getState())
                {
                    launcherFrame.forceUpdate = true;
                }
                launcherFrame.login(userName.getText());
            }

        }
);
    }

    private void readUsername()
    {
        try
        {
            File file = new File(MinecraftUtil.getWorkingDirectory(), "lastlogin");
            Cipher cipher = getCipher(2, "passwordfile");
            DataInputStream datainputstream;
            if(cipher != null)
            {
                datainputstream = new DataInputStream(new CipherInputStream(new FileInputStream(file), cipher));
            } else
            {
                datainputstream = new DataInputStream(new FileInputStream(file));
            }
            userName.setText(datainputstream.readUTF());
            datainputstream.close();
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
        try
        {
            File file = new File(MinecraftUtil.getWorkingDirectory(), "jarfile");
            DataInputStream datainputstream;
            datainputstream = new DataInputStream(new FileInputStream(file));
            boxcombo.select(datainputstream.readUTF());
            
            datainputstream.close();
        }
        catch(Exception exception)
        {
        	boxcombo.select("minecraf.jar");
            exception.printStackTrace();
        }
    }

    private void writeUsername()
    {
        try
        {
            File file = new File(MinecraftUtil.getWorkingDirectory(), "lastlogin");
            Cipher cipher = getCipher(1, "passwordfile");
            DataOutputStream dataoutputstream;
            if(cipher != null)
            {
                dataoutputstream = new DataOutputStream(new CipherOutputStream(new FileOutputStream(file), cipher));
            } else
            {
                dataoutputstream = new DataOutputStream(new FileOutputStream(file));
            }
            dataoutputstream.writeUTF(userName.getText());
            dataoutputstream.writeUTF("");
            dataoutputstream.close();
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }

        try
        {
            File file = new File(MinecraftUtil.getWorkingDirectory(), "jarfile");
            DataOutputStream dataoutputstream;
                dataoutputstream = new DataOutputStream(new FileOutputStream(file));
            
            dataoutputstream.writeUTF(boxcombo.getSelectedItem());
            dataoutputstream.writeUTF("");
            dataoutputstream.close();
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
        }
    }

    private Cipher getCipher(int i, String s)
        throws Exception
    {
        Random random = new Random(0x29482c2L);
        byte abyte0[] = new byte[8];
        random.nextBytes(abyte0);
        PBEParameterSpec pbeparameterspec = new PBEParameterSpec(abyte0, 5);
        javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(s.toCharArray()));
        Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
        cipher.init(i, secretkey, pbeparameterspec);
        return cipher;
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {

        int i = getWidth() / 2;
        int j = getHeight() / 2;
        if(img == null || img.getWidth() != i || img.getHeight() != j)
        {
            img = createVolatileImage(i, j);
        }
        Graphics g1 = img.getGraphics();
        for(int k = 0; k <= i / 32; k++)
        {
            for(int l = 0; l <= j / 32; l++)
            {
                g1.drawImage(bgImage, k * 32, l * 32, null);
            }

        }

        g1.setColor(Color.LIGHT_GRAY);
        String s = "Minecraft Launcher";
        g1.setFont(new Font(null, 1, 20));
        FontMetrics fontmetrics = g1.getFontMetrics();
        g1.drawString(s, i / 2 - fontmetrics.stringWidth(s) / 2, j / 2 - fontmetrics.getHeight() * 2);
        g1.dispose();
        g.drawImage(img, 0, 0, i * 2, j * 2, null);
    }

    private Panel buildLoginPanel()
    {
        Panel panel = new Panel() {

            private static final long serialVersionUID = 1L;
            private Insets insets;

            public Insets getInsets()
            {
                return insets;
            }

            public void update(Graphics g)
            {
                paint(g);
            }

            public void paint(Graphics g)
            {
                super.paint(g);
                int i = 0;
                g.setColor(Color.BLACK);
                g.drawRect(0, 0 + i, getWidth() - 1, getHeight() - 1 - i);
                g.drawRect(1, 1 + i, getWidth() - 3, getHeight() - 3 - i);
                g.setColor(Color.WHITE);
                g.drawRect(2, 2 + i, getWidth() - 5, getHeight() - 5 - i);
            }

            
            {
                insets = new Insets(12, 24, 16, 32);
            }
        }
;
        panel.setBackground(Color.GRAY);
        BorderLayout borderlayout = new BorderLayout();
        borderlayout.setHgap(0);
        borderlayout.setVgap(8);
        panel.setLayout(borderlayout);
        GridLayout gridlayout = new GridLayout(0, 1);
        GridLayout gridlayout1 = new GridLayout(0, 1);
        gridlayout.setVgap(2);
        gridlayout1.setVgap(2);
        Panel panel1 = new Panel(gridlayout);
        Panel panel2 = new Panel(gridlayout1);
        panel1.add(new Label("Username:", 2));
        panel1.add(new Label("Select *.jar file:", 2));
        panel1.add(new Label("", 2));
        panel2.add(userName);
        panel.add(panel1, "West");
        panel.add(panel2, "Center");
        panel2.add(boxcombo);
        panel2.add(forceUpdateBox);
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new BorderLayout());
        Panel panel5 = new Panel(new BorderLayout());
        openSettings.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent actionevent)
            {
            	launcherFrame.showSettings();
            }

        }
);
        panel4.add(openSettings);
        try
        {
            if(outdated)
            {
                Label label = new Label("You need to update the launcher!") {

                    private static final long serialVersionUID = 0L;

                    public void paint(Graphics g)
                    {
                        super.paint(g);
                        int i = 0;
                        int j = 0;
                        FontMetrics fontmetrics = g.getFontMetrics();
                        int k = fontmetrics.stringWidth(getText());
                        int l = fontmetrics.getHeight();
                        if(getAlignment() == 0)
                        {
                            i = 0;
                        } else
                        if(getAlignment() == 1)
                        {
                            i = getBounds().width / 2 - k / 2;
                        } else
                        if(getAlignment() == 2)
                        {
                            i = getBounds().width - k;
                        }
                        j = (getBounds().height / 2 + l / 2) - 1;
                        g.drawLine(i + 2, j, (i + k) - 2, j);
                    }

                    public void update(Graphics g)
                    {
                        paint(g);
                    }

                }
;
                label.setCursor(Cursor.getPredefinedCursor(12));
                label.addMouseListener(new MouseAdapter() {

                    public void mousePressed(MouseEvent mouseevent)
                    {
                        try
                        {
                            Desktop.getDesktop().browse((new URL("http://www.minecraft.net/download.jsp")).toURI());
                        }
                        catch(Exception exception)
                        {
                            exception.printStackTrace();
                        }
                    }

                }
);
                label.setForeground(Color.BLUE);
                panel5.add(label, "West");
                panel5.add(new Panel(), "Center");
            } else
            {
            	
                panel5.add(creditsVersion, "West");
                panel5.add(new Panel(), "Center");
            }
        }
        catch(Error error) { }
        panel3.add(panel4, "Center");
        panel3.add(launchButton, "East");

      
        panel3.add(panel5, "West");
        panel.add(panel3, "South");
        errorLabel.setFont(new Font(null, 2, 16));
        errorLabel.setForeground(new Color(0x800000));
        panel.add(errorLabel, "North");
        return panel;
    }

    public void setError(String s)
    {
        removeAll();
        add(buildLoginPanel());
        errorLabel.setText(s);
        validate();
    }

    public void loginOk()
    {
        writeUsername();
    }



}
